package dataPersistence.dto;



import hitExceptions.InvalidDataException;


import java.util.ArrayList;

import coreModel.Barcode;
import coreModel.HIT;
import coreModel.ProductContainer;
import coreModel.ProductGroup;
import coreModel.StorageUnit;

/**
 * Inherits from DataTransferObject
 *
 */
public class StorageUnitDTO extends ProductContainerDTO implements DataTransferObject<StorageUnit>
{

	public StorageUnitDTO(StorageUnit storageunit) 
	{
		convertFromModelObject(storageunit);
	}
	
	

	public StorageUnitDTO() 
	{
		//do nothing
	}

	@Override
	public StorageUnit convertToModelObject()
	{
		ProductContainer su=null;
		try
		{
			su = new StorageUnit(super.getName());
			su.setContainerID(new Barcode(super.getProductContainerID()));
			ArrayList<String> children =super.getChildrenIds();

			
			for(String pc:	children)
			{
				su.addProductGroup((ProductGroup) HIT.getInstance().getStorageUnitManager().getContainerByID(pc));

			}
		}

		catch (InvalidDataException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return (StorageUnit) su;

	}

	@Override
	public void convertFromModelObject(StorageUnit modelObject)
	{
		super.setProductContainerID(modelObject.getID());
		super.setParentID(null);
		super.setName(modelObject.getName());
		ArrayList<String> list = new ArrayList<String>();
		for(ProductContainer pc : modelObject.getChildren())
		{
			list.add(pc.getID().toString());
		}
		super.setChildrenIds(list);

	}

}
